﻿

#ifndef TREEVIEWWITHCUSTOMWIDGET_H
#define TREEVIEWWITHCUSTOMWIDGET_H

#include <QWidget>
#include <QStyledItemDelegate>
#include <QCheckBox>
#include <QHeaderView>
#include <QPainter>
#include <QLineEdit>

namespace Ui {
class CTreeViewWithCustomWidget;
}

class QStandardItemModel;

class TestWidget : public QLineEdit
{
public:
    TestWidget(QWidget* parent):QLineEdit(parent){}
    ~TestWidget(){
        int i = 0;
    }
};

/**
 * @brief 树节点自定义委托
 */
class TreeVarDelegate : public QStyledItemDelegate
{
    Q_OBJECT
    using QStyledItemDelegate::QStyledItemDelegate;
public:
    TreeVarDelegate(QObject *parent = 0);
    /**
     * @brief paint
     * @param painter
     * @param option
     * @param index
     */
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QWidget *createEditor(QWidget *parent,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
};

class CheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
public:
    CheckBoxHeaderView(int checkColumnIndex,
                       Qt::Orientation orientation,
                       QWidget * parent = 0) :
        QHeaderView(orientation, parent)
    {
        // 默认ComboBox;
        m_checkbox = new QCheckBox("123123",this);
        m_checkbox->setIconSize(QSize(4,4));
        m_checkbox->setStyleSheet("background:#aaaaff;spacing:13px;");
        //m_checkbox->setStyleSheet("background:white;border-right:1px solid #eee;");
    }
protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    {
        auto rc = viewport()->rect();
        int d = logicalIndexAt(0,0);
        int d2 = logicalIndexAt(rc.right(),0);
        if(d > 1 || d2 < 1)
        {
            m_checkbox->hide();

        }
        if (logicalIndex == 0)
        {
            QHeaderView::paintSection(painter, rect, logicalIndex);
            //painter->fillRect(rect, Qt::white);
            auto rc = m_checkbox->geometry();
            auto rc2 = rect;
            rc.moveCenter(rc2.center());
            m_checkbox->show();
            m_checkbox->setGeometry(rect);
        }
        else
        {
            QHeaderView::paintSection(painter, rect, logicalIndex);
        }
    }
private:
    QCheckBox *m_checkbox = nullptr;
};

class CTreeViewWithCustomWidget : public QWidget
{
    Q_OBJECT

public:
    explicit CTreeViewWithCustomWidget(QWidget *parent = nullptr);
    ~CTreeViewWithCustomWidget();

protected:
    bool eventFilter(QObject *watched, QEvent *event) override;

private slots:
    void on_treeView_clicked(const QModelIndex& index);

    void on_pushButton_clicked();

private:
    void InitTreeView();

private:
    Ui::CTreeViewWithCustomWidget *ui;

    QStandardItemModel* m_pTreeModel = nullptr;
};

#endif // TREEVIEWWITHCUSTOMWIDGET_H
